﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Drawing;
using System.Collections.ObjectModel;
using Allocine_Lib;

namespace DCCiné
{
    /// <summary>
    /// Logique d'interaction pour RechercheFilm.xaml
    /// </summary>
    public partial class RechercheFilm : Window
    {
        private Films FilmsList = null;

        public RechercheFilm()
        {
            InitializeComponent();
        }

        public void ChercheFilm(string critere)
        {
            
            //List<Film> ListeFilm = new List<Film>();
            //Films ListeFilm = null;
            //_FilmsCollection.Clear();
            //FilmsList =
            this.FilmsList = Allocine.RechercheFilm(critere);
            this.DataContext = this.FilmsList;
            //this.DataContextChanged
            //_FilmsCollection.Clear();

            //ListeSalles.Sort();


            //foreach (Film _Film in ListeFilm)
            //{
            //    _FilmsCollection.Add(_Film);
            //}

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ChercheFilm(SAI_Critere.Text);
  
        }

        //public ObservableCollection<Film> FilmsCollection
        //{ get { return _FilmsCollection; } }

    }

    /// <summary>
    /// Converter use to convert a System.Drawing.Image to an BitmapImage, when you need an ImageSource
    /// </summary>
    public class DrawingImageToImageSourceConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            BitmapImage ImgSource = null;

            System.Drawing.Image image = value as System.Drawing.Image;

            if (image != null)
            {
                ImgSource = new BitmapImage();
                ImgSource.BeginInit();

                ImgSource.StreamSource = new MemoryStream(ConvertImageToByteArray(image));

                ImgSource.EndInit();
            }

            return ImgSource;
        }

        /// <summary>
        /// Convert a System.Drawing.Image to a byte array
        /// </summary>
        /// <param name="img">The System.Drawing.Image to convert in a byte array</param>
        /// <returns>A byte array</returns>
        private byte[] ConvertImageToByteArray(System.Drawing.Image img)
        {
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

            return ms.ToArray();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }

}
